package com.quectel.core.module.partybuildingnotice.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.quectel.constant.global.SystemConstants;
import com.quectel.core.constants.RedisCacheConstants;
import com.quectel.core.module.partybuilding.dto.PartyOrganizationDto;
import com.quectel.core.module.partybuilding.service.PartyOrganizationService;
import com.quectel.core.module.partybuildingnotice.dao.PartyBuildingNoticeDao;
import com.quectel.core.module.partybuildingnotice.dao.PartyBuildingNoticePartyBindingDao;
import com.quectel.core.module.partybuildingnotice.dto.PartyBuildingNoticeDto;
import com.quectel.core.module.partybuildingnotice.dto.PartyBuildingNoticePartyBindingDto;
import com.quectel.core.module.partybuildingnotice.entity.PartyBuildingNoticeEntity;
import com.quectel.core.module.partybuildingnotice.entity.PartyBuildingNoticePartyBindingEntity;
import com.quectel.core.module.partybuildingnotice.service.PartyBuildingNoticePartyBindingService;
import com.quectel.core.module.partybuildingnotice.service.PartyBuildingNoticeService;
import com.quectel.util.common.CopyUtils;
import com.quectel.util.kit.CacheKit;
import com.quectel.util.kit.Snowflake;
import com.quectel.util.mybatis.MyBatisPlusUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;
/**
 *
 * @author bob
 * @email bob.yu@quectel.com
 * @date 2023/05/24 11:38
 */
@DubboService
public class PartyBuildingNoticeServiceImpl implements PartyBuildingNoticeService {

    private static final Logger LOGGER = LoggerFactory.getLogger(PartyBuildingNoticeServiceImpl.class);

    /**
     * 入参解释:实体id
     */
    private static final Function<Long, String> PARTY_BUILDING_NOTICE_CACHE_BY_ID_FUNC = id -> String.format(RedisCacheConstants.PROJECT_NAME + "PARTY_BUILDING_NOTICE_CACHE_BY_ID:%d", id);

    /**
     * 清除缓存方法
     */
    private static final Consumer<PartyBuildingNoticeDto> CLEAR_CACHE_FUNC = dto -> {
        CacheKit.invalidRedisCache(PARTY_BUILDING_NOTICE_CACHE_BY_ID_FUNC.apply(dto.getId()));
    };

    @Autowired
    private PartyBuildingNoticeDao partyBuildingNoticeDao;
    @Autowired
    private PartyBuildingNoticePartyBindingDao noticePartyBindingDao;
    @Autowired
    private PartyBuildingNoticePartyBindingService noticePartyBindingService;
    @Autowired
    private PartyOrganizationService partyOrganizationService;


    /**
     * save or update
     *
     * @param partyBuildingNoticeDto
     */
    @Override
    public void saveOrUpdate(PartyBuildingNoticeDto partyBuildingNoticeDto) {
        if (partyBuildingNoticeDto.getId() == null) {
            Long id = save(partyBuildingNoticeDto);
            partyBuildingNoticeDto.setId(id);
        } else {
            updateById(partyBuildingNoticeDto);
        }

        // 删除绑定关系
        noticePartyBindingDao.delete(new LambdaQueryWrapper<PartyBuildingNoticePartyBindingEntity>()
                .eq(PartyBuildingNoticePartyBindingEntity::getNoticeId, partyBuildingNoticeDto.getId()));

        // 绑定下党组织信息到中间表
        for (PartyOrganizationDto organization : partyBuildingNoticeDto.getPartyOrganizationList()) {
            PartyBuildingNoticePartyBindingDto noticePartyBindingDto = new PartyBuildingNoticePartyBindingDto();
            noticePartyBindingDto.setId(Snowflake.nextId());
            noticePartyBindingDto.setNoticeId(partyBuildingNoticeDto.getId());
            noticePartyBindingDto.setPartyId(organization.getId());
            noticePartyBindingService.save(noticePartyBindingDto);
        }
    }

    @Override
    public PartyBuildingNoticeDto selectCacheById(Long id) {
        return CacheKit.cacheToRedis(
                () -> selectById(id),
                PARTY_BUILDING_NOTICE_CACHE_BY_ID_FUNC.apply(id),
                SystemConstants.NOT_NULL_CACHE_EXPIRE_SECONDS
        );
    }

    @Override
    public PartyBuildingNoticeDto selectById(Long id){
            PartyBuildingNoticeEntity entity = partyBuildingNoticeDao.selectById(id);
        return paddingField(CopyUtils.copyObj(entity,PartyBuildingNoticeDto.class));
    }

    @Override
    public List<PartyBuildingNoticeDto> queryList(Map<String,Object> params){
        List<PartyBuildingNoticeEntity> list = partyBuildingNoticeDao.queryList(params);
        List<PartyBuildingNoticeDto> result =  CopyUtils.copyList(list,PartyBuildingNoticeDto.class);
        result.forEach(this::paddingField);
        return result;
    }

    /**
     * 此方法慎用 禁止填充List一类 比如一个人有多个地址这里不允许填充
     * 填充的原则是: 1:被填充对象不经常变换 2:根据id可以走缓存 3数据足够简单 比如设备类型 所属园区等
     *
     * @param dto
     * @return
     */
    private PartyBuildingNoticeDto paddingField(PartyBuildingNoticeDto dto){
        if (dto != null){
            if (dto.getPartyId()!=null){
                PartyOrganizationDto organizationDto = partyOrganizationService.selectCacheById(dto.getPartyId());
                if (organizationDto != null){
                    dto.setPartyName(organizationDto.getName());
                }
            }
        }
        return dto;
    }

    @Override
    public int queryTotal(Map<String, Object> params){
        return partyBuildingNoticeDao.queryTotal(params);
    }

    @Override
    public Long save(PartyBuildingNoticeDto dto){
        PartyBuildingNoticeEntity entity = CopyUtils.copyObj(dto,PartyBuildingNoticeEntity.class);
        entity.setId(Snowflake.nextId());
        partyBuildingNoticeDao.insert(entity);
        return entity.getId();
    }

    @Override
    public void updateById(PartyBuildingNoticeDto dto){
        PartyBuildingNoticeEntity entity = CopyUtils.copyObj(dto,PartyBuildingNoticeEntity.class);

        partyBuildingNoticeDao.updateById(entity);

        CLEAR_CACHE_FUNC.accept(dto);
    }

    @Override
    public void updateAllColumnById(PartyBuildingNoticeDto dto) {
       PartyBuildingNoticeEntity entity = CopyUtils.copyObj(dto,PartyBuildingNoticeEntity.class);

       MyBatisPlusUtils.updateAllColumnById(entity, partyBuildingNoticeDao);

       CLEAR_CACHE_FUNC.accept(dto);
    }

    @Override
    public void deleteById(Long id){

        CLEAR_CACHE_FUNC.accept(selectById(id));

        partyBuildingNoticeDao.deleteById(id);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] ids){
        for (Long id : ids) {
            deleteById(id);
        }
    }
}
